package com.rpp.service.impl;

import com.rpp.annotation.Autowired;
import com.rpp.annotation.Service;
import com.rpp.annotation.Transactional;
import com.rpp.dao.AccountDao;
import com.rpp.pojo.Account;
import com.rpp.service.TransferService;

/**
 * @Description
 * @Author rpp
 * @Date 2020/6/25 9:56 下午
 */
@Service
public class TransferServiceImpl implements TransferService {

    //容器自动注入
    @Autowired
    private AccountDao accountDao;

    @Override
    public Account findByCardNo(String cardNo) throws Exception {
        Account account = accountDao.queryAccountByCardNo(cardNo);
        return account;
    }

    /**
     * @Description 转账
     **/
    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        //查询账户余额
        Account fromAccount = accountDao.queryAccountByCardNo(fromCardNo);
        Account toAccount = accountDao.queryAccountByCardNo(toCardNo);
        //扣减、增加余额
        fromAccount.setMoney(fromAccount.getMoney() - money);
        toAccount.setMoney(toAccount.getMoney() + money);
        accountDao.updateAccountByCardNo(fromAccount);

        //异常，事务回滚
//        int i = 10 / 0;

        accountDao.updateAccountByCardNo(toAccount);

    }
}
